home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 August / August CD.bin / Shareware / Education / numericalmethods Folder / chap_7 / a7_1.m next >
Encoding:
Text File  |  1994-06-05  |  2.0 KB  |  98 lines  |  [MATS/MATL]

  1. echo off;
  2. % NUMERICAL METHODS: MATLAB Programs, (c) John H. Mathews 1994
  3. % To accompany the text:
  4. % NUMERICAL METHODS for Mathematics, Science and Engineering, 2nd Ed, 1992
  5. % Prentice Hall, Englewood Cliffs, New Jersey, 07632, U.S.A.
  6. % This free software is complements of the author.
  7.  
  8. % Algorithm 7.1 (Composite Trapezoidal Rule).
  9. % Section    7.2,    Composite Trapezoidal and Simpson's Rule, Page 365
  10. echo on; clc; format long; hold off; clear
  11.  
  12. % This program implements the trapezoidal rule.
  13.  
  14. % The function f(x) is integrated over the interval [a,b].
  15.  
  16. %    Define and store the function f(x)    in the M-file  f.m 
  17. % function y = f(x)
  18. % y = 1 + exp(-x).*sin(4*x);
  19.  
  20. delete f.m
  21. diary f.m; disp('function y = f(x)');...
  22.            disp('y = 1 + exp(-x).*sin(4*x);');...
  23. diary off;
  24.  
  25. f(0); % Remark. f.m traprl.m grpoly.m are used for Algorithm 7.1
  26.  
  27. pause    % Press any key to continue.
  28.  
  29. clc;, clg;
  30. %    Plot f(x) over the interval [a,b].
  31.  
  32. a = 0;
  33. b = 1;
  34. h = (b-a)/200;
  35. X = a:h:b;
  36. Y = f(X);
  37.  
  38. pause    % Press any key to see the graph y = f(x).
  39.  
  40. clc; clg;
  41. c = 0;
  42. d = 2;
  43. axis([a b c d]);...
  44. plot(X,Y,'g');...
  45. hold on;...
  46. plot([a b],[0 0],'b',[0 0],[c d],'b');...
  47. xlabel('x');...
  48. ylabel('y');...
  49. title('The curve y = f(x) over [a,b].');...
  50. grid;...
  51. axis;...
  52. hold off;...
  53. shg; pause    % Press any key to continue.
  54.  
  55. clc;
  56.  
  57. % The trapezoidal rule is applied over the interval [a,b].
  58.  
  59. %    Place the endpoints of [a,b] in  a  and  b.
  60.  
  61. %    Place the number of subintervals in  m.
  62.  
  63. a  = 0;
  64. b  = 1;
  65. m  = 8;
  66.  
  67. s = traprl('f',a,b,m);
  68.  
  69. pause    % Press any key to continue.
  70.  
  71. clc;
  72. Mx1 = 'The trapezoidal rule applied with m = ';
  73. Mx2 = [Mx1,num2str(m),' subintervals is:'];
  74. clc,disp(''),disp(Mx2),disp(''),disp(s)
  75.  
  76. pause    % Press any key to see a nice graph y = f(x).
  77.  
  78. clc; clg;
  79. c = 0;
  80. d = 2;
  81. axis([a b c d]);...
  82. plot(X,Y,'g');...
  83. hold on;...
  84. plot([a b],[0 0],'b',[0 0],[c d],'b');...
  85. xlabel('x');...
  86. ylabel('y');...
  87. title('The trapezoidal rule.');...
  88. if m <= 50,
  89.   grpoly(a,b,m,1);
  90. end;...
  91. grid;...
  92. axis;...
  93. hold off;...
  94. shg; pause    % Press any key to continue.
  95.  
  96. clc,echo off,diary output,...
  97. disp(''),disp(Mx2),disp(''),disp(s),diary off,echo on
  98.